package com.bodu.core.util;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;


/**
 * 数据来源判断
 */
public class HttpSourceUtil {

    /**
     * android : 所有android设备 mac os : iphone ipad windows
     * phone:Nokia等windows系统的手机
     *
     * @param request
     * @return
     */
    public static boolean isMobileDevice(HttpServletRequest request) {
        String requestHeader = request.getHeader("user-agent").toLowerCase();
        String[] deviceArray = new String[]{"android", "iphone", "ios", "windows phone"};
        if (requestHeader == null) {
            return false;
        }
        requestHeader = requestHeader.toLowerCase();
        for (int i = 0; i < deviceArray.length; i++) {
            if (requestHeader.indexOf(deviceArray[i]) > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * android : 所有android设备 mac os : iphone ipad windows
     * phone:Nokia等windows系统的手机
     *
     * @param request 请求头
     * @return
     */
    public static String getDevice(HttpServletRequest request) {
        String requestHeader = request.getHeader("user-agent").toLowerCase();
        String[] deviceArray = new String[]{"android", "iphone", "ios", "windows phone"};
        if (requestHeader == null) {
            return "PC端";
        }
        requestHeader = requestHeader.toLowerCase();
        for (int i = 0; i < deviceArray.length; i++) {
            if (requestHeader.indexOf(deviceArray[i]) > 0) {
                return "WAP端";
            }
        }
        return "PC端";
    }

    /**
     * 判断是否为微信
     *
     * @param request
     * @return
     */
    public static boolean isWeChat(HttpServletRequest request) {
        //判断 是否是微信浏览器
        String userAgent = request.getHeader("user-agent").toLowerCase();
        if (userAgent.indexOf("micromessenger") > -1) {//微信客户端
            return true;
        } else {
            return false;
        }
    }

    /**     * 获取本机IP	 */
    public static String getLocalIP () {
        String ip = "";
        try {
            if (System.getProperty("os.name").toLowerCase().contains("linux")) {
                Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
                while (e1.hasMoreElements()) {
                    NetworkInterface ni = (NetworkInterface) e1.nextElement();
                    if (!ni.getName().equals("eth0")) {
                        continue;
                    } else {
                        Enumeration<?> e2 = ni.getInetAddresses();
                        while (e2.hasMoreElements()) {
                            InetAddress ia = (InetAddress) e2.nextElement();
                            if (ia instanceof Inet6Address) {
                                continue;
                            }
                            ip = ia.getHostAddress();
                        }
                        break;
                    }
                }
            } else {
                ip = InetAddress.getLocalHost().getHostAddress().toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ip;
    }
}
